package com.dfbz.netctoss.service.impl;

import com.dfbz.netctoss.entity.Account;
import com.dfbz.netctoss.entity.Bill;
import com.dfbz.netctoss.mapper.AccountMapper;
import com.dfbz.netctoss.mapper.BillMapper;
import com.dfbz.netctoss.service.AccountService;
import com.dfbz.netctoss.service.BillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BillServiceImpl implements BillService {

    @Autowired
    BillMapper mapper;

    //注入solrService
    @Autowired
    SolrClient client;

    @Override
    public PageInfo<Bill> selectPage(int pageNum, int pageSize, String keywords) {
        PageHelper.startPage(pageNum,pageSize);
        List<Bill> bills = selectByKeywords(keywords);
        return new PageInfo<>(bills);
    }

    public List<Bill> selectByKeywords(String keywords) {
        List<Bill> bills;
        try{
            SolrQuery solrQuery = new SolrQuery();
            //查询复制域
            if(StringUtils.isEmpty(keywords) || keywords.equals("")){
                solrQuery.set("q","*:*");
                QueryResponse response = client.query(solrQuery);
                bills = response.getBeans(Bill.class);
            }else{
                solrQuery.set("q","keyword:" + keywords);
                //开启高亮
                solrQuery.setHighlight(true);

                //添加高亮域
                solrQuery.addHighlightField("accountName");
                solrQuery.addHighlightField("idcardNo");

                //前缀
                solrQuery.setHighlightSimplePre("<font color='red'>");

                //后缀
                solrQuery.setHighlightSimplePost("</font>");

                QueryResponse response = client.query(solrQuery);
                bills = response.getBeans(Bill.class);

                //获取高亮数据
                Map<String, Map<String, List<String>>> high = response.getHighlighting();

                for (Bill bill : bills) {

                    if(high.get(bill.getBillId()).get("accountName")!=null){
                        //将查询到的数据换成高亮数据
                        String accountName = high.get(bill.getBillId()).get("accountName").get(0);
                        bill.setAccountName(accountName);
                    }
                    if(high.get(bill.getBillId()).get("idcardNo")!=null){
                        String idcardNo = high.get(bill.getBillId()).get("idcardNo").get(0);
                        //将查询到的数据换成高亮数据
                        bill.setIdcardNo(idcardNo);
                    }

                }

            }
//            solrQuery.setQuery("keywords:" + keywords);


            return bills;

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
